package com.jz.yanshe.ui.fragment;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.jz.yanshe.ui.VipIntroduceActivity;
import com.jz.yanshe.ui.WXLoginActivity;
import com.jz.yanshe.base.BaseFragment;
import com.jz.yanshe.eventbus.FragmentTabEvent;
import com.jz.yanshe.ui.MyAddressActivity;
import com.jz.yanshe.ui.MyBankActivity;
import com.jz.yanshe.ui.MyOrderActivity;
import com.jz.yanshe.ui.UploadVipFileActivity;
import com.jz.yanshe.util.ImageLoaderHelper;
import com.jz.jd.rest.business.RestProxy;
import com.jz.jd.rest.exception.BusinessException;
import com.jz.jd.rest.response.VipInfoResponse;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

import com.jz.yanshe.ui.VipActivity;
import com.jz.yanshe.util.PreferencesHelper;
import com.jz.yanshe.R;

/**
 * Created by Administrator on 2018/2/26.
 */

public class MineFragment extends BaseFragment {

    @BindView(R.id.tv_user_name)
    TextView tv_user_name;

    @BindView(R.id.tv1)
    TextView tv1;

    @BindView(R.id.tv2)
    TextView tv2;


    @BindView(R.id.btn_access_vip)
    TextView btn_access_vip;

    @BindView(R.id.img_avatar)
    ImageView img_avatar;

    Context mContext;


    public static MineFragment newInstance() {
        return new MineFragment();
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.fragment_mine;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {
        getVipInfo();
    }

    @OnClick(R.id.btn_unpaid)
    void btn_unpaid() {
        Bundle bundle = new Bundle();
        bundle.putInt("id", 1);
        startActivity(MyOrderActivity.class, bundle);
    }

    @Override
    public void onAttach(Context context) {
        mContext = context;
        super.onAttach(context);
    }

    @OnClick(R.id.btn_dfh)
    void btn_dfh() {
        Bundle bundle = new Bundle();
        bundle.putInt("id", 2);
        startActivity(MyOrderActivity.class, bundle);
    }

    @OnClick(R.id.btn_dsh)
    void btn_dsh() {
        Bundle bundle = new Bundle();
        bundle.putInt("id", 3);
        startActivity(MyOrderActivity.class, bundle);
    }

    @OnClick(R.id.btn_sh)
    void btn_sh() {
        Bundle bundle = new Bundle();
        bundle.putInt("id", 4);
        startActivity(MyOrderActivity.class, bundle);
    }


    @OnClick(R.id.btn_all)
    void btn_all() {
        Bundle bundle = new Bundle();
        bundle.putInt("id", 0);
        startActivity(MyOrderActivity.class, bundle);
    }

    @OnClick(R.id.btn_address)
    void btn_address() {
        Bundle bundle = new Bundle();
        bundle.putInt("id", 1);
        startActivity(MyAddressActivity.class, bundle);
    }

    @OnClick(R.id.btn_bank)
    void btn_bank() {
        startActivity(MyBankActivity.class);
    }

    @OnClick(R.id.btn_vip)
    void btn_vip() {
        startActivity(VipActivity.class);
    }


    @OnClick(R.id.btn_exit)
    void btn_exit() {
        startActivity(WXLoginActivity.class);
        PreferencesHelper.getInstance().set(getActivity(), "token", "");
//        PreferencesHelper.getInstance().set(getActivity(),"user_type","");
//        getActivity().finish();
        EventBus.getDefault().post(new FragmentTabEvent("1"));
    }

        @OnClick(R.id.btn_access_vip)
    void btn_access_vip(){
            Bundle bundle = new Bundle();
            bundle.putString("userType","2");
        startActivity(UploadVipFileActivity.class,bundle);
    }
    @OnClick(R.id.btn_vip_detail)
    void btn_vip_detail() {
        startActivity(VipIntroduceActivity.class);
    }


    private void getVipInfo() {
        RestProxy.getInstance().getVipInfo(PreferencesHelper.getInstance().getString(getActivity(), "token"), new Observer<VipInfoResponse>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(VipInfoResponse vipInfoResponse) {
                tv_user_name.setText("用户名：" + vipInfoResponse.map.nickname);
                ImageLoaderHelper.getInstance().GlideCircleImageLoader(mContext, vipInfoResponse.map.head_url, img_avatar, R.mipmap.img_hot_default);
//                PreferencesHelper.getInstance().set(getActivity(),"user_type",vipInfoResponse.map.user_type+"");
                if (vipInfoResponse.map.user_type == 2) {
                    btn_access_vip.setVisibility(View.GONE);
                    tv2.setVisibility(View.VISIBLE);
                    tv1.setText("会员邀请码:" + vipInfoResponse.map.invite_code);
                }
                if (isAdded()) {
                    if (vipInfoResponse.map.confirmStatus.get(0).payment_credential_state == 1) {
                        btn_access_vip.setBackgroundColor(getResources().getColor(R.color.commonGrey));
                        btn_access_vip.setOnClickListener(null);
                    }
                    if (vipInfoResponse.map.confirmStatus.get(0).payment_credential_state == 3 || vipInfoResponse.map.confirmStatus.get(0).payment_credential_state == 0) {
                        btn_access_vip.setBackgroundColor(getResources().getColor(R.color.commonRed));
//                        btn_access_vip.setOnClickListener(new View.OnClickListener() {
//                            @Override
//                            public void onClick(View v) {
//                                startActivity(UploadVipFileActivity.class);
//                            }
//                        });
                    }
                }
            }

            @Override
            public void onError(Throwable e) {
                if (e instanceof BusinessException) {
                    EventBus.getDefault().post(new FragmentTabEvent("1"));
                    toLogin(((BusinessException) e).errorCode);
                }
            }

            @Override
            public void onComplete() {

            }
        });
    }
}
